<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogOpen"
    append-to-body
    width="600px"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="dv-form p-10">
      <el-row v-if="type !=='add' || form.parentId">
        <el-col :span="24">
          <el-form-item label="父级类型" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="menuOptions"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择父级类型"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="字典名称" prop="label">
            <el-input v-model.trim="form.label" placeholder="请输入" :maxlength="40" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字典编码" prop="code">
            <el-input v-model.trim="form.code" placeholder="请输入" :maxlength="40" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="序号" prop="dictSort">
            <el-input-number v-model.trim="form.dictSort" :max="999999999" controls-position="right" :min="0" class="w-100" @blur="(e)=>blurNumber(e,'dictSort')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="v in statusList"
                :key="v.value"
                :label="v.value"
              >{{ v.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="drawer-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="debounceSaveHandle()">确 定</el-button>
    </div>
  </el-dialog>

</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { save, edit, getInfo } from '@/api/system/treeDict'
import { debounceBy } from '@/utils'
export default {
  components: { Treeselect },
  props: {
    menuOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      title: '',
      dialogOpen: false,
      rules: {
        label: [
          { required: true, message: '字典名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '字典编码不能为空', trigger: 'blur' }
        ],
        dictSort: [
          { required: true, message: '序号不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      form: {},
      statusList: [
        { value: '0', label: '启用' },
        { value: '1', label: '停用' }
      ],
      debounceSaveHandle: null,
      type: ''
    }
  },
  methods: {
    reset(type, parentId) {
      this.type = type
      this.debounceSaveHandle = debounceBy(this.submitForm, 1000, this)
      this.form = {
        id: undefined,
        label: undefined,
        code: undefined,
        parentId: undefined,
        dictSort: 0,
        status: '0'
      }
      this.resetForm('form')
      this.dialogOpen = true
      this.title = type === 'add' ? '新增' : type === 'edit' ? '编辑' : '详情'
      if (type !== 'add') {
        this.getInfoHandle(parentId)
      } else {
        this.form.parentId = parentId
      }
    },
    getInfoHandle(id) {
      getInfo({ id: id }).then(res => {
        this.form = res.data
      })
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length || node.children == null || node.children == 'null') {
        delete node.children
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children
      }
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            edit(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.cancel()
              this.$emit('resetList')
            })
          } else {
            save(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.cancel()
              this.$emit('resetList')
            })
          }
        }
      })
    },
    cancel() {
      this.resetForm('form')
      this.dialogOpen = false
    }
  }
}
</script>

<style scoped lang='scss'>

</style>
